package com.asset.dispoal.commom.version.strategy.execute;

import com.asset.dispoal.commom.version.SqlExecute;
import com.asset.dispoal.commom.version.strategy.VersionStrategyServer;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerParam;
import tech.mhuang.pacebox.core.strategy.StrategyHandlerResult;

import java.util.Arrays;
import java.util.List;


/**
 * @author qr
 * @date 2022/2/18$
 */
@Component
public class UpdateStrategyServer implements VersionStrategyServer {
    @Override
    public StrategyHandlerResult<String> strategy(StrategyHandlerParam<String> strategyHandlerParam) {
        String sql = strategyHandlerParam.getData();
        Long version = queryVersion(sql);
        String tableName = sql.substring(sql.toUpperCase().indexOf("UPDATE") + 7, sql.toUpperCase().indexOf("SET"));
        String[] result = sql.substring(sql.toUpperCase().indexOf("SET") + 4, sql.toUpperCase().indexOf("WHERE")).split(",");
        this.setVersion(sql, tableName);
        StringBuffer key = new StringBuffer();
        StringBuffer value = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            final String[] split = result[i].split("=");
            key.append(split[0]).append(",");
            value.append(split[1]).append(",");
        }
        key.append(" version");
        value.append(++version);
        StringBuffer executeSql = new StringBuffer("insert into ");
        executeSql.append(tableName.trim()).append("_version").append(" (").
                append(key).append(" ) ").append("VALUES ").
                append(" ( ").append(value).append(" ) ");
        return new StrategyHandlerResult<String>(true, executeSql.toString());
    }

    private Long queryVersion(String sql) {
        String tableName = sql.substring(sql.toUpperCase().indexOf("UPDATE") + 7, sql.toUpperCase().indexOf("SET"));
        String result = sql.substring(sql.toUpperCase().indexOf("WHERE") + 6);
        String querySql = "select " + tableName + " from  where  " + result;
        final Long version = SqlExecute.queryVersion(querySql);
        return version;
    }

    private void setVersion(String sql, String tableName) {
        Long version = queryVersion(sql);
        final String where = sql.substring(sql.toUpperCase().indexOf("WHERE") + 6);
        final String updateSql = "update " + tableName + "set version  = " + (++version) + " " + where;
        SqlExecute.updateSql(updateSql);
    }


    @Override
    public List<String> event() {
        return Arrays.asList("UPDATE");
    }

    public static void main(String[] args) {

        String sql = "UPDATE 表名称 SET 列名称 = 新值  ,列名称1 = 某值2  WHERE 列名称 = 某值 ";
        String result = sql.substring(sql.toUpperCase().indexOf("WHERE") + 6);
        final String where = sql.substring(sql.toUpperCase().indexOf("WHERE"));
        System.out.println(where);
        //  final String substring = sql.substring(sql.toUpperCase().indexOf("INTO")+5 , sql.toUpperCase().indexOf("("));
        //      String [] result = sql.substring(sql.toUpperCase().indexOf("SET") + 4, sql.toUpperCase().indexOf("WHERE")).split(",");
        //   System.out.println(substring);
//        System.out.println(result.length);
//        System.out.println(result[0] + "      "+result[1]  );
    }
}
